﻿using Vowei.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Vowei.Core.Models;
using System.Linq;
using Vowei.Data.Models;
using Vowei.Core;
using Vowei.Data.Helpers;

namespace vowei.test
{
    [TestClass()]
    public class SecurityTest : TestBase
    {
        [TestMethod()]
        public void CheckPermissionBasicTest()
        {
            var roleName = "CheckPermissionBasicTest_Role";
            using (var context = new VoweiContextImpl())
            {
                var acl = context.Acl;
                var pset = context.PermissionSet.First(
                    p => p.Title == Constants.BuiltIn.PermissionSet.ManageProjectPermissionSet);

                // 避免重复添加
                if (acl.Any(a => a.RoleName == roleName))
                {
                    var aces = acl.Where(a => a.RoleName == roleName);
                    foreach (var ace in aces)
                        acl.Remove(ace);
                }

                var entry = new Ace()
                {
                    RoleName = roleName,
                };
                entry.Permissions.Add(pset);
                acl.Add(entry);
                context.SaveChanges();
            }

            using (var context = new VoweiContextImpl())
            {
                var ace = context.Acl.First(a => a.RoleName == roleName);
                Assert.IsNotNull(ace.Permissions);
                Assert.AreEqual(1, ace.Permissions.Count);
            }
        }

        [TestMethod]
        public void CheckBuiltInPermissionSetContainsPermissionTest()
        {
            using (var context = new VoweiContextImpl())
            {
                var pset = context.PermissionSet.First(
                    p => p.Title == Constants.BuiltIn.PermissionSet.ManageProjectPermissionSet);

                Assert.IsTrue(pset.Authorized(Constants.Permission.Read.Project));
                Assert.IsTrue(pset.Authorized(Constants.Permission.Write.Project));
                Assert.IsTrue(pset.Authorized(Constants.Permission.Read.Requirement));
                Assert.IsTrue(pset.Authorized(Constants.Permission.Write.Permission));
                Assert.IsTrue(pset.Authorized(Constants.BuiltIn.PermissionSet.ManageProjectPermissionSet));
                Assert.IsFalse(pset.Authorized(Constants.BuiltIn.PermissionSet.ProjectMemeberPermissionSet));
            }
        }

        [TestMethod()]
        public void CheckProjectManagerPermissionBasicTest()
        {
            var roleName = "CheckProjectManagerPermissionBasicTest_Role";
            var roles = new string[] { roleName };
            using (var context = new VoweiContextImpl())
            {                
                var acl = context.Acl;
                var pset = context.PermissionSet.First(
                    p => p.Title == Constants.BuiltIn.PermissionSet.ManageProjectPermissionSet);

                // 避免重复添加
                if (acl.Any(a => a.RoleName == roleName))
                {
                    var aces = acl.Where(a => a.RoleName == roleName);
                    foreach (var ace in aces)
                        acl.Remove(ace);
                }

                var entry = new Ace()
                {
                    RoleName = roleName,
                };
                entry.Permissions.Add(pset);
                acl.Add(entry);
                context.SaveChanges();
            }

            using (var context = new VoweiContextImpl())
            {
                Assert.IsTrue(context.Acl.CheckPermission(roles, Constants.Permission.Read.Project));
                Assert.IsTrue(context.Acl.CheckPermission(roles, Constants.Permission.Write.Project));
                Assert.IsTrue(context.Acl.CheckPermission(roles, Constants.Permission.Read.Permission));
                Assert.IsTrue(context.Acl.CheckPermission(roles, Constants.Permission.Read.PermissionSet));
                Assert.IsTrue(context.Acl.CheckPermission(roles, Constants.Permission.Write.Permission));
                Assert.IsTrue(context.Acl.CheckPermission(roles, Constants.Permission.Write.PermissionSet));
                Assert.IsTrue(context.Acl.CheckPermission(roles, Constants.Permission.Read.Comment));

                Assert.IsTrue(context.Acl.CheckPermission(roles,
                    Constants.BuiltIn.PermissionSet.ManageProjectPermissionSet));
            }
        }
    }
}
